﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using TS = TransactionServer.General_Classes;
using Microsoft.Xrm.Sdk;

namespace TransactionServer.CRMActivities
{

    public sealed class CRMUpdate : CodeActivity
    {
        // Define an activity input argument of type string
        public InArgument<TS.Context> Context { get; set; }
        public InArgument<TS.Entity> Entity { get; set; }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            TS.Entity Entity = context.GetValue(this.Entity);
            TS.Context con = context.GetValue(this.Context);
            CRMConnector conn = (CRMConnector)con.Connector[Entity.System];

            try
            {
                Entity crmEntity = conn.ConvertFrom(Entity);

                TS.Event evnt = new TS.Event(DateTime.Now, "Update", Entity);

                conn.crmService.Update(crmEntity);

                context.SetValue(this.Entity, Entity);
            }
            catch (Exception ex)
            {
                throw new TS.SystemOperationException(Entity, ex);
            }
        }
    }
}
